package com.yupi.springbootinit.manager;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.datasource.*;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.model.dto.post.PostQueryRequest;
import com.yupi.springbootinit.model.dto.search.SearchRequest;
import com.yupi.springbootinit.model.dto.user.UserQueryRequest;
import com.yupi.springbootinit.model.entity.Picture;
import com.yupi.springbootinit.model.enums.SearchTypeEnum;
import com.yupi.springbootinit.model.vo.PostVO;
import com.yupi.springbootinit.model.vo.SearchVO;
import com.yupi.springbootinit.model.vo.UserVO;
import com.yupi.springbootinit.service.PictureService;
import com.yupi.springbootinit.service.PostService;
import com.yupi.springbootinit.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
@RequiredArgsConstructor
public class SearchFacade {

    private final UserDataSource userDataSource;
    private final PostDataSource postDataSource;
    private final PictureDataSource pictureDataSource;

    private final DataSourceRegistry dataSourceRegistry;

    public SearchVO searchAll(@RequestBody SearchRequest searchRequest, HttpServletRequest request){
        String type = searchRequest.getType();
        SearchTypeEnum searchTypeEnum = SearchTypeEnum.getEnumByValue(type);
        final String value = searchTypeEnum.getValue();
        ThrowUtils.throwIf(StringUtils.isBlank(value), ErrorCode.PARAMS_ERROR);//传入的值是非法的
        SearchVO searchVO = new SearchVO();
        final String searchText = searchRequest.getSearchText();
        if(type==null){
            UserQueryRequest queryRequest = new UserQueryRequest();
            queryRequest.setUserName(searchText);
            final Page<UserVO> userVOPage = userDataSource.doSearch(searchText,1,10);
            PostQueryRequest postQueryRequest = new PostQueryRequest();
            postQueryRequest.setSearchText(searchText);
            final Page<PostVO> postVOPage = postDataSource.doSearch(searchText,1,10);
            final Page<Picture> picturePage = pictureDataSource.doSearch(searchText, 1, 10);
            searchVO.setUserList(userVOPage.getRecords());
            searchVO.setPostList(postVOPage.getRecords());
            searchVO.setPictureList(picturePage.getRecords());
        }else {
            DataSource dataSource = dataSourceRegistry.getDataSource(type);
            final Page page = dataSource.doSearch(searchText, 1, 10);
            final List<?> records = page.getRecords();
            searchVO.setDatasourceList(page.getRecords());
        }
        return searchVO;
    }
}
